package com.cloud.referenceTest;

import java.lang.ref.WeakReference;
import java.util.concurrent.TimeUnit;

/**
 * 弱引用
 * 被弱引用关联的对象只能生存到下一次垃圾收集发生为止
 * 当垃圾收集开始工作，无论当前内存是否足够，都会回收掉只被弱引用关联的对象
 */
public class WeakReferenceTest {
    public static void main(String[] args) {
        WeakReference<Student> studentWeakReference = new WeakReference<>(new Student());
        System.out.println(studentWeakReference.get());
        System.gc();
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(studentWeakReference.get());
    }
}

